/**
 * 参数设置管理初始化
 */
var RechargeList = {
    id: "RechargeListTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};




RechargeList.formatDate = function (shijianchuo) {

	var add0 = function add0(m){return m<10?'0'+m:m }

	if(shijianchuo=="" || shijianchuo == undefined)return null;
	//shijianchuo是整数，否则要parseInt转换
	var time = new Date(shijianchuo);
	var y = time.getFullYear();
	var m = time.getMonth()+1;
	var d = time.getDate();
	var h = time.getHours();
	var mm = time.getMinutes();
	var s = time.getSeconds();
	return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}

RechargeList.formatDate_8 = function (shijianchuo) {
	var add0 = function add0(m){return m<10?'0'+m:m }

	if(shijianchuo=="" || shijianchuo == undefined)return null;
	//shijianchuo是整数，否则要parseInt转换
	var time = new Date(shijianchuo);
	var y = time.getFullYear();
	var m = time.getMonth()+1;
	var d = time.getDate();
	var h = time.getHours()-8;
	var mm = time.getMinutes();
	var s = time.getSeconds();
	return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}

/**
 * 初始化表格的列
 */
RechargeList.initColumn = function () {
	
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '记录号', field: 'recharge_no', visible: false, align: 'center', valign: 'middle',width:'50px'},
        {title: '状态', field: 'status', align: 'center', valign: 'middle',
        	formatter: function (value, row, index) {//自定义显示，这三个参数分别是：value该行的属性，row该行记录，index该行下标
        		var data;
        		switch(value)
        		{
        		case 0: data="订单生成"; break;
        		// case 1: data="待转账"; break;
        		// case 2: data="已充值，待审核"; break;
                case 1: data="未跟单"; break;
                case 2: data="已充值，待审核"; break;
        		case 3: data="转账失败"; break;
        		case 4: data="已完成"; break;
        		case 5: data="已取消"; break;
        		default: data =row.status;break;
        		}
                return data;
            }
        },
        {title: '申请时间', field: 'add_time', align: 'center', valign: 'middle',
        	formatter: function (value, row, index) {
                  console.log(row.add_time);
        		return RechargeList.formatDate(row.add_time);
        	}
        },
        {title: '确认转账时间', field: 'confirm_transfer_time', align: 'center', valign: 'middle',
        	formatter: function (value, row, index) {
    		return RechargeList.formatDate(row.confirm_transfer_time);
    	}
        },
        {title: '绑定手机', field: 'binding_phone', align: 'center', valign: 'middle'},
        {title: '通证名称', field: 'currency', align: 'center', valign: 'middle'},
        {title: '充值数量', field: 'amount', align: 'center', valign: 'middle'},
        {title: '转账地址', field: 'remarkInfo', align: 'center', valign: 'middle'},
        {title: '实际收款', field: 'actual_amount', align: 'center', valign: 'middle'},
        {title: '跟单人账号', field: 'doc_account', align: 'center', valign: 'middle'},
        {title: '跟单时间', field: 'doc_time', align: 'center', sortable: true,valign: 'middle',
        	formatter: function (value, row, index) {
        		return RechargeList.formatDate(row.doc_time);
        	}
        },
        {title: '审核时间', field: 'audit_time', align: 'center', valign: 'middle',
        	formatter: function (value, row, index) {
        		return RechargeList.formatDate(row.audit_time);
        	}
        },
        
        /*{
            title: '操作',
            field: 'recharge_no',
            align: 'center',
            formatter: function (value, row, index) {//自定义显示可以写标签哦~
                return '<div><a href="#" mce_href="#" onclick="edit(\'' + row.recharge_no + '\')">查看详情</a> <a href="#" mce_href="#" onclick="edit(\'' + row.recharge_no + '\')">跟单</a> <a href="#" mce_href="#" onclick="edit(\'' + row.recharge_no + '\')">操作</a> </div>';
            }
        }*/

    ]
    return columns;
};

/**
 * 检查是否选中
 */
RechargeList.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        RechargeList.seItem = selected[0];
        return true;

    }
};

/**
 * 检查是否可以停用
 */
RechargeList.checkstopuse = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    RechargeList.seItem = selected[0];
    if(RechargeList.seItem.zhuangtai == "停用"){
        Feng.info("这个合伙人状态已经是停用");
        return false;
    }else{
        RechargeList.seItem = selected[0];
        return true;

    }
};

/**
 * 检查是否可以启用
 */
RechargeList.checkuse = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    RechargeList.seItem = selected[0];
    if(RechargeList.seItem.zhuangtai == "启用"){
        Feng.info("这个合伙人状态已经是启用");
        return false;
    }else{
        return true;
    }
};

/**
 * 停用确认框
 */
RechargeList.RechargeStopUse = function () {
    if(this.check()) {
        if(this.checkstopuse()) {
        $.confirm({
            title: '系统提示',
            content: '是否确定停用此合伙人,登录账号:' + RechargeList.seItem.binding_phone,
            buttons: {
                ok: {
                    text: '确认',
                    btnClass: 'btn-primary',
                    action: RechargeList.stopUse()
                },
                cancel: {
                    text: '取消',
                    btnClass: 'btn-primary',
                    action: function () {
                        // button action.
                    }
                }
            }
        });
    }
    }
};

/**
 * 启用确认框
 */
RechargeList.RechargeUse = function () {
    if(this.check()) {
        if(this.checkuse()) {
        $.confirm({
            title: '系统提示',
            content: '是否确定启用此合伙人,登录账号:' + RechargeList.seItem.binding_phone,
            buttons: {
                ok: {
                    text: '确认',
                    btnClass: 'btn-primary',
                    action: RechargeList.Use()
                },
                cancel: {
                    text: '取消',
                    btnClass: 'btn-primary',
                    action: function () {
                        // button action.
                    }
                },
            }
        });
     }
    }
};

/**
 * 打开查看参数设置详情
 */
RechargeList.RechargeUpdate = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '审核',
            area: ['800px', '720px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/rechargeList/recharge_update/' + RechargeList.seItem.recharge_no
        });
        this.layerIndex = index;
    }
};

/**
 * 查看合同详情
 */
RechargeList.RechargeDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '合同详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/rechargeList/rechargeDetail/' + RechargeList.seItem.recharge_no
        });
        this.layerIndex = index;
    }
};

/**
 * 跟单
 */
RechargeList.toDoc = function () {
	if (this.check()) {
		
		var ajax = new $ax(Feng.ctxPath + "/rechargeList/doc", function (data) {
	    	if(data["code"]==2){
	    		Feng.error(data["message"] + "!");
	    	}else{
	        Feng.success("跟单成功!");
	        window.parent.RechargeList.table.refresh();
	    	}
		}, function (data) {
			Feng.error("失败!" + data.responseJSON.message + "!");
		});
		ajax.set("recharge_no", this.seItem.recharge_no);
		ajax.start();
		
	}
};
/**
 * 停用
 */
RechargeList.StopUse = function () {
    if (this.check()) {

            var ajax = new $ax(Feng.ctxPath + "/rechargeList/stopUse", function (data) {
                Feng.success("停用成功!");
                RechargeList.table.refresh();
            }, function (data) {
                Feng.error("失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", this.seItem.id);
            ajax.start();

    }
};

/**
 * 启用
 */
RechargeList.Use = function () {
    if (this.check()) {

            var ajax = new $ax(Feng.ctxPath + "/rechargeList/use", function (data) {
                Feng.success("启用成功!");
                RechargeList.table.refresh();
            }, function (data) {
                Feng.error("失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", this.seItem.id);
            ajax.start();

    }
};

/**
 * 查询参数设置列表
 */
RechargeList.search = function () {
    var queryData = {};
    queryData['binding_phone'] = $("#binding_phone").val();
    queryData['status'] = $("#status").val();
    queryData['disUsername'] = $("#disUsername").val();
    queryData['add_time'] = RechargeList.formatDate_8($("#add_time").val());//$("#add_time").val().replace("/","-");
    if($("#add_time").val() != "") {
        if($("#add_time_end").val() == "") {
            Feng.info("请选择结束时间！");
            return false;
        }else{
            queryData['add_time_end'] = RechargeList.formatDate_8($("#add_time_end").val());//$("#add_time_end").val().replace("/","-");
        }
    }
    RechargeList.table.refresh({query: queryData});
};

$(function () {
    $("#add_time").attr('type','date');
    $("#add_time_end").attr('type','date');
    var defaultColunms = RechargeList.initColumn();
    var table = new BSTable(RechargeList.id, "/rechargeList/list", defaultColunms);
    table.setPaginationType("server");
//    table.setQueryParams();
    RechargeList.table = table.init();
});



